// pages/hospital/hospital.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    imgs: [],
    chooseLoaction: 0,
    location: '',
    longitude: 123.287682,
    latitude: 44.124004,
    is_special: [
      {
        value: 0,
        name: '否',
        selected: false
      },
      {
        value: 1,
        name: '是',
        selected: false
      },
    ],
    typeArr: [
      { value: 0, name: '请选择' },
      { value: 1, name: '集体企业' },
      { value: 2, name: '外资企业' },
      { value: 3, name: '私营企业' },
      { value: 4, name: '行政事业单位' },
      { value: 5, name: '个体户' },
      { value: 6, name: '其他企业' },
    ],
    typeIdx: 0,
    flag: 0,
    flag1: 0,
    rawData: [],//后台获取的原始数组
    multiObjArray: [], //整理后在页面循环的数组
    multiIndex: [0, 0],
    comArr: [],
    gridArr: [],
    newArr: [],
    g_id: '',
    c_id: '',
    tempImg: '',
    id: ''
  },

  onLoad(opts) {
    console.log(opts)
    this.setData({
      id: opts.id
    })
    wx.showLoading({
      title: '加载中...',
    })
  },

  onShow() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      status: 1
    })
    this.loadInfo()
  },

  loadInfo() {
    let that = this
    let access_token = wx.getStorageSync('access_token')
    let id = that.data.id
    wx.request({
      url: 'https://baokang.wshoto.com/wx/basicinfo',
      data: {
        access_token: access_token,
        type: 3,
        id: id
      },
      success: (res) => {
        console.log(res)
        let images = []
        images.push(res.data.info.pic)
        if (res.data.code == 1) {
          that.setData({
            flag1: 1,
            listInfo: res.data.info,
            typeIdx: res.data.info.type,
            images: images
          })
          if (res.data.info.is_special == 0) {
            that.data.is_special[0].selected = true;
            that.data.is_special[1].selected = false
            that.setData({
              is_special: that.data.is_special
            })
          } else {
            that.data.is_special[0].selected = false;
            that.data.is_special[1].selected = true
            that.setData({
              is_special: that.data.is_special
            })
          }
        }
        wx.hideLoading()
      }
    })
  },

  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    let data = {
      multiIndex: this.data.multiIndex,
      multiObjArray: this.data.multiObjArray
    }
    data.multiIndex[e.detail.column] = e.detail.value
    switch (e.detail.column) {
      case 0:
        for (let i in this.data.comArr) {
          if (data.multiIndex[0] == i) {
            data.multiObjArray[1] = this.data.newArr[i]
          }
        }
        data.multiIndex[1] = 0;
        break
    }
    this.setData(data)
  },

  bindMultiPickerChange(e) {
    console.log(e)
    let comIdx = e.detail.value[0]
    let multiIndex = e.detail.value[1]
    let c_id = this.data.rawData[comIdx].c_id
    let g_id = this.data.gridArr[comIdx][multiIndex].g_id
    this.setData({
      multiIndex: e.detail.value,
      g_id: g_id,
      c_id: c_id,
      flag: 1
    })
  },

  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    let item1 = this.data.is_special
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  typeChange(e) {
    console.log(e)
    this.setData({
      typeIdx: e.detail.value,
      flag1: 1
    })
    console.log(this.data.typeArr[this.data.typeIdx].value)
  },

  formSubmit(e) {
    console.log(e)
    let access_token = wx.getStorageSync('access_token')
    let { c_name, is_special, worker_num, cover_area, legal_person, legal_person_phone, party_clerk, party_clerk_phone, labour_union_master, labour_union_master_phone, labour_vip_num, party_num, safe_master, safe_master_phone, culture_remark, address, remark } = e.detail.value
    let type = this.data.typeArr[this.data.typeIdx].value
    if (e.detail.value.c_name == '') {
      wx.showToast({
        title: '单位名称不能为空！',
        icon: 'none'
      })
      return false
    }
    if (type == '') {
      wx.showToast({
        title: '请选择单位类型！',
        icon: 'none'
      })
      return false
    }
    if (worker_num == '') {
      wx.showToast({
        title: '员工人数不能为空！',
        icon: 'none'
      })
      return false
    }
    if (legal_person == '') {
      wx.showToast({
        title: '法人代表不能为空！',
        icon: 'none'
      })
      return false
    }
    if (legal_person_phone == '') {
      wx.showToast({
        title: '法人联系电话不能为空！',
        icon: 'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    if (this.data.id) {
      wx.request({
        url: 'https://baokang.wshoto.com/wx/companyadd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          id: this.data.id,
          access_token: access_token,
          // c_id: c_id,
          // g_id: g_id,
          type: type,
          c_name: c_name,
          is_special: is_special,
          worker_num: worker_num,
          cover_area: cover_area,
          legal_person: legal_person,
          legal_person_phone: legal_person_phone,
          party_clerk: party_clerk,
          party_clerk_phone: party_clerk_phone,
          labour_union_master: labour_union_master,
          labour_union_master_phone: labour_union_master_phone,
          labour_vip_num: labour_vip_num,
          party_num: party_num,
          safe_master: safe_master,
          safe_master_phone: safe_master_phone,
          culture_remark: culture_remark,
          address: address,
          remark: remark,
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 1) {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })

    } else {
      wx.request({
        url: 'https://baokang.wshoto.com/wx/companyadd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          access_token: access_token,
          type: type,
          c_name: c_name,
          is_special: is_special,
          worker_num: worker_num,
          cover_area: cover_area,
          legal_person: legal_person,
          legal_person_phone: legal_person_phone,
          party_clerk: party_clerk,
          party_clerk_phone: party_clerk_phone,
          labour_union_master: labour_union_master,
          labour_union_master_phone: labour_union_master_phone,
          labour_vip_num: labour_vip_num,
          party_num: party_num,
          safe_master: safe_master,
          safe_master_phone: safe_master_phone,
          culture_remark: culture_remark,
          address: address,
          remark: remark,
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 1) {
            wx.showModal({
              title: '提示',
              content: '提交成功',
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })
    }
  }
})